home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / ScreenResolution / policykit.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  2.9 KB  |  73 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''
  5. Helper class to do authentication via PolicyKit
  6. '''
  7. import dbus
  8. import os
  9. import logging
  10.  
  11. class PolicyKitAuthentication(object):
  12.     '''
  13.     Helper class to do authentication via PolicyKit
  14.     '''
  15.     
  16.     def __init__(self):
  17.         super(PolicyKitAuthentication, self).__init__()
  18.         self._PolicyKitAuthentication__pk = None
  19.         self._PolicyKitAuthentication__pa = None
  20.  
  21.     
  22.     def is_authorized(self, action_id, pid = None):
  23.         '''
  24.         Ask PolicyKit whether we are already authorized.
  25.         '''
  26.         if pid == None:
  27.             pid = os.getpid()
  28.         
  29.         pid = dbus.UInt32(pid)
  30.         authorized = self.policy_kit.IsProcessAuthorized(action_id, pid, False)
  31.         logging.debug('%s: authorized=%r', action_id, authorized)
  32.         return 'yes' == authorized
  33.  
  34.     
  35.     def obtain_authorization(self, action_id, widget = None):
  36.         '''
  37.         Try to obtain authoriztation for the specified action.
  38.         '''
  39.         if self.is_authorized(action_id):
  40.             return True
  41.         if not widget or widget.get_toplevel().window.xid:
  42.             pass
  43.         xid = 0
  44.         xid = dbus.UInt32(xid)
  45.         pid = dbus.UInt32(os.getpid())
  46.         granted = self.auth_agent.ObtainAuthorization(action_id, xid, pid)
  47.         logging.debug('%s: granted=%r', action_id, granted)
  48.         return bool(granted)
  49.  
  50.     
  51.     def __get_policy_kit(self):
  52.         '''Retreive the D-Bus interface of PolicyKit.'''
  53.         if self._PolicyKitAuthentication__pk:
  54.             return self._PolicyKitAuthentication__pk
  55.         service = dbus.SystemBus().get_object('org.freedesktop.PolicyKit', '/')
  56.         self._PolicyKitAuthentication__pk = dbus.Interface(service, 'org.freedesktop.PolicyKit')
  57.         return self._PolicyKitAuthentication__pk
  58.  
  59.     
  60.     def __get_auth_agent(self):
  61.         '''
  62.         Retreive the D-Bus interface of the PolicyKit 
  63.         authentication agent.
  64.         '''
  65.         if self._PolicyKitAuthentication__pa:
  66.             return self._PolicyKitAuthentication__pa
  67.         self._PolicyKitAuthentication__pa = dbus.SessionBus().get_object('org.freedesktop.PolicyKit.AuthenticationAgent', '/')
  68.         return self._PolicyKitAuthentication__pa
  69.  
  70.     auth_agent = property(__get_auth_agent)
  71.     policy_kit = property(__get_policy_kit)
  72.  
  73.